package com.cpit.qualitycontrol.service.impl;

import com.cpit.platform.common.constant.SystemConstant;
import com.cpit.platform.common.entity.TableDataQueryCondition;
import com.cpit.platform.common.entity.TableJsonData;
import com.cpit.qualitycontrol.dao.jpqlquery.SubTaskReportRepository;
import com.cpit.qualitycontrol.entity.SubTaskReport;
import com.cpit.qualitycontrol.service.SubTaskReportService;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class SubTaskReportServiceImpl implements SubTaskReportService {
    @Autowired
    SubTaskReportRepository subTaskReportRepository;
    @Override
    public List<SubTaskReport> findByCondition(int page, int size, String order, String orderBy, String beginTime, String endTime, Long productLineId, String projectName, String constructionTeamNames, Long deviceTypeId, Long workOrderType) {

        if (orderBy == null || orderBy.length() == 0) {
            orderBy = "id";
        }
        Sort.Direction der = order.equals(SystemConstant.DIRECTION_ASC) ? Sort.Direction.ASC : Sort.Direction.DESC;
        PageRequest request = new PageRequest(page - 1, size, new Sort(der, orderBy));


        return subTaskReportRepository.findByCondition( beginTime,  endTime, productLineId,  projectName, constructionTeamNames, deviceTypeId, workOrderType,  request).getContent();
    }

//    @Override
//    public TableJsonData findByConditionJqg(TableDataQueryCondition cond) {
////        PageRequest request = ServiceUtils.getPageRequestByTableDataQueryCondition(cond);
////
////        Page<SubTaskReport> ret = this.subTaskReportRepository.findByCondition(beginTime, endTime, Long productLineId, String projectName, String constructionTeamNames, Long deviceTypeId, Long workOrderType, Pageable pageable) {);
//
//        return new TableJsonData(cond, ret.getTotalElements(), ret.getContent());
//    }

    @Override
    public List<SubTaskReport> findAll() {
        return null;
    }

    public List<SubTaskReport> findAllByCondition(String beginTime, String endTime, Long productLineId, String projectName, String constructionTeamNames, Long deviceTypeId, Long workOrderType){
      return  subTaskReportRepository.findByCondition(beginTime,  endTime,  productLineId,  projectName, constructionTeamNames,  deviceTypeId, workOrderType)  ;
    }

    @Override
    public TableJsonData findSubTaskByConditionJqg(Long id, TableDataQueryCondition cond) {
        PageRequest request = ServiceUtils.getPageRequestByTableDataQueryCondition(cond);
        Map queryCondMap = cond.getQueryCond();
        String productLineIdStr = (String) queryCondMap.get("productLineId");
        Long productLineId = null;
        if (StringUtil.isNotEmpty(productLineIdStr)) {
            productLineId = Long.valueOf(productLineIdStr);
        }
/**
 *  'productLineId': _productLineId,
 *                     'projectName': projectName,
 *                    'constructionTeamName': constructionTeamName,
 *                     'beginTime': beginTime,
 *                     'endTime': endTime,
 *                    'deviceTypeId': deviceTypeId,
 *                     'workOrderType': workOrderType
 */
        String projectName = (String) queryCondMap.get("projectName");
        String constructionTeamName = (String) queryCondMap.get("constructionTeamName");
//
//        if (isConstructionTeam(userId)) {//施工队角色
//            constructionTeamName = organizationRepository.findById(userRepository.findById(userId).getOrganizationId()).getName();
//        }


//        String deviceTypeIdStr = (String) queryCondMap.get("deviceTypeId");
//        Long deviceTypeId = null;
//        if (StringUtil.isNotEmpty(deviceTypeIdStr)) {
//            deviceTypeId = Long.valueOf(deviceTypeIdStr);
//        }
        String beginTime = (String) queryCondMap.get("beginTime");

        String endTime = (String) queryCondMap.get("endTime");

        String sworkOrderType = (String) queryCondMap.get("workOrderType");
        Long workOrderType =null;
        if (StringUtil.isNotEmpty(sworkOrderType)) {
            workOrderType = Long.valueOf(sworkOrderType);
        }

        Page<SubTaskReport> ret = this.subTaskReportRepository.findByCondition(beginTime,  endTime,  productLineId,  projectName, constructionTeamName,  null, workOrderType,request);

        return new TableJsonData(cond, ret.getTotalElements(), ret.getContent());
    }
}
